package com.bdqn.sys.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.common.utitls.DateGindViewResult;
import com.bdqn.common.utitls.JSONResult;
import com.bdqn.common.utitls.SystemConstants;
import com.bdqn.sys.entity.Permission;
import com.bdqn.sys.service.PermissionService;
import com.bdqn.sys.vo.PermissionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2020-04-26
 */
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {


    @Resource
    private PermissionService permissionService;


    /**
     * 点击后右侧显示权限信息
     */
    @RequestMapping("/list")
    public DateGindViewResult list(PermissionVo permissionVo){
        //创建分页信息
        IPage<Permission> page=new Page<Permission>(permissionVo.getPage(),permissionVo.getLimit());
        //创建条件构造器
        QueryWrapper<Permission> queryWrapper=new QueryWrapper<Permission>();
        queryWrapper.eq("type", SystemConstants.Type_PERMISSION);//只查询权限
        //权限名称查询
        queryWrapper.like(StringUtils.isNotBlank(permissionVo.getTitle()),"title",permissionVo.getTitle());
        //权限编码
        queryWrapper.like(StringUtils.isNotBlank(permissionVo.getPercode()),"percode",permissionVo.getPercode());
        //权限编号
        queryWrapper.eq(permissionVo.getId()!=null,"id",permissionVo.getId())
                .or().eq(permissionVo.getId()!=null,"pid",permissionVo.getId());
        //调用分页查询的方法
        permissionService.page(page,queryWrapper);
        //返回查询结果
        return  new DateGindViewResult(page.getTotal(),page.getRecords());//总页数 总记录数

    }

    /**
     * 权限添加
     */
    @RequestMapping("/addPermission")
    public JSONResult addPermission(Permission permission){
        //设置权限类型
        permission.setType(SystemConstants.Type_PERMISSION);//权限类型
        if(permissionService.save(permission)){
            return SystemConstants.Add_SUCCESS;
        }
        return SystemConstants.Add_ERROR;
    }



    /**
     * 权限修改
     */
    @RequestMapping("/updatePermission")
    public JSONResult updatePermission(Permission permission){
        if(permissionService.updateById(permission)){
            return SystemConstants.UPDATE_SUCCESS;
        }
        return SystemConstants.UPDATE_ERROR;
    }


    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public JSONResult deleteById(int id){
        if (permissionService.removeById(id)){
            return SystemConstants.DELETE_SUCCESS;
        }
        return SystemConstants.DELETE_ERROR;
    }
}

